package cn.sinata.wanglin.neighbor.fragment;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.fragment.RecyclerViewFragment;
import com.xilada.xldutils.utils.ActivityUtil;

import java.util.ArrayList;

import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.NeighborNearbyAdapter;
import cn.sinata.wanglin.model.Village;
import cn.sinata.wanglin.neighbor.activity.VillageNeighborActivity;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

/**
 *
 * Created by liaoxiang on 16/12/8.
 */

public class NeighborNearbyFragment extends RecyclerViewFragment {

    private NeighborNearbyAdapter neighborNearbyAdapter;
    private ArrayList<Village> mVillages = new ArrayList<>();
    private int pageNo = 0;

    @Override
    protected RecyclerView.Adapter setAdapter() {
        neighborNearbyAdapter = new NeighborNearbyAdapter(mVillages);
        View headerView = View.inflate(context, R.layout.header_neighbor_nearby_layout,null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        headerView.setLayoutParams(params);
        neighborNearbyAdapter.setHeaderView(headerView);
        neighborNearbyAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Village village = mVillages.get(position);
                ActivityUtil.create(context)
                        .go(VillageNeighborActivity.class)
                        .put("id",village.getId())
                        .put("name",village.getHousingName())
                        .start();
            }
        });
        return neighborNearbyAdapter;
    }

    @Override
    protected void onFirstVisibleToUser() {
        showDialog();
        getData();
    }

    void getData(){

        HttpManager.getNearbyHousing(MyApplication.lng,MyApplication.lat,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<Village>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<Village> villages) {
                        if (pageNo == 0) {
                            mVillages.clear();
                        }
                        if (villages.size() == 0) {
                            if (pageNo == 0) {
                                setLoadMoreText("暂无数据");
                            } else {
                                pageNo --;
                                setLoadMoreText("没有更多了");
                            }
                        }
                        mVillages.addAll(villages);
                        neighborNearbyAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    protected void onVisibleToUser() {

    }

    @Override
    protected void onInvisibleToUser() {

    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData();
    }
}
